﻿#region Copyright (c) 2011, thinktecture (http://www.thinktecture.com)

/*
   thinktecture.UIAnnotations was developed by Jörg Neumann.
   Copyright (c) 2011, thinktecture (http://www.thinktecture.com).
   All rights reserved, comes as-is and without any warranty. Use of this
   source file is governed by the license which is contained in LICENSE.TXT 
   in the distribution.
*/

#endregion

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace Thinktecture.DataForm.Controls
{
    /// <summary>
    /// Displays busy state.
    /// </summary>
    [TemplateVisualState(GroupName="CommonStates", Name="IsBusy")]
    public class BusyIndicator : Control
    {

        #region Constructors
        
        static BusyIndicator()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BusyIndicator), new FrameworkPropertyMetadata(typeof(BusyIndicator)));
            Timeline.DesiredFrameRateProperty.OverrideMetadata(
                typeof(Timeline),
                    new FrameworkPropertyMetadata { DefaultValue = 35 });
        }

        #endregion

        #region Properties

        #region IsBusy

        public static readonly DependencyProperty IsBusyProperty =
            DependencyProperty.Register("IsBusy", typeof(bool), typeof(BusyIndicator), new UIPropertyMetadata(false, OnIsBusyPropertyChanged));
        public bool IsBusy { get { return (bool)GetValue(IsBusyProperty); } set { SetValue(IsBusyProperty, value); } }

        private static void OnIsBusyPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
                VisualStateManager.GoToState((BusyIndicator)sender, "IsBusy", true);
            else
                VisualStateManager.GoToState((BusyIndicator)sender, "Normal", true);
        }
        
        #endregion

        #region IndicatorType

        public static readonly DependencyProperty IndicatorTypeProperty =
            DependencyProperty.Register("IndicatorType", typeof(IndicatorType), typeof(BusyIndicator), new UIPropertyMetadata(IndicatorType.Wheel, OnIndicatorTypePropertyChanged));
        public IndicatorType IndicatorType { get { return (IndicatorType)GetValue(IndicatorTypeProperty); } set { SetValue(IndicatorTypeProperty, value); } }

        private static void OnIndicatorTypePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((BusyIndicator)sender).OnIndicatorTypeChanged(sender, e);
        }

        private void OnIndicatorTypeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var type = (IndicatorType)e.NewValue;
            if (type == IndicatorType.Wheel)
            {
                var resourceLocater =
                    new Uri("/Thinktecture.DataForm;component/Themes/generic.xaml", UriKind.Relative);
                ResourceDictionary dict = Application.LoadComponent(resourceLocater) as ResourceDictionary;
                this.Template = (ControlTemplate)dict["IndicatorWheelTemplate"];
            }
            else if (type == IndicatorType.Dots)
            {
                var resourceLocater =
                    new Uri("/Thinktecture.DataForm;component/Themes/generic.xaml", UriKind.Relative);
                ResourceDictionary dict = Application.LoadComponent(resourceLocater) as ResourceDictionary;
                this.Template = (ControlTemplate)dict["IndicatorDotsTemplate"];
            }
        }

        #endregion

        #region IndicatorTemplate

        public static readonly DependencyProperty IndicatorTemplateProperty =
            DependencyProperty.Register("IndicatorTemplate", typeof(ControlTemplate), typeof(BusyIndicator), new UIPropertyMetadata(null, OnIndicatorTemplatePropertyChanged));
        public ControlTemplate IndicatorTemplate { get { return (ControlTemplate)GetValue(IndicatorTemplateProperty); } set { SetValue(IndicatorTemplateProperty, value); } }

        private static void OnIndicatorTemplatePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((BusyIndicator)sender).OnIndicatorTemplateChanged(sender, e);
        }

        private void OnIndicatorTemplateChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var template = e.NewValue as ControlTemplate;
            if (template != null)
                this.Template = template;
        }

        #endregion

        #endregion

    }

    #region IndicatorType
    
    /// <summary>
    /// Defines values for busy incicator styles.
    /// </summary>
    public enum IndicatorType
    {
        Wheel,
        Dots,
        Custom
    }
    
    #endregion

}
